package jin.si.controller;

import jin.si.pojo.JsBeforeUser;
import jin.si.service.LoginService;
import jin.si.util.JsonData;
import jin.si.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @projectName: jinsi-java
 * @package: jin.si.controller
 * @className: LoginController
 * @date: 2022/2/27 13:42:51
 * @author: WYH
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Value("${fdfspath}")
    private String path;

    /**
     * @Author WYH
     * @Description //TODO 手机号和密码登录
     * @Date 下午 1:37 2022/3/3
     * @Param phone，password
     * @return JsonData
     **/
    @PostMapping("/getLogin")
    public JsonData getLogn(String phone, String password){
        JsBeforeUser logn = loginService.getLogn(phone);
        boolean verify = false;
        if (logn != null){
            verify = Md5Util.verify(password, logn.getPassword());
        }
        if (verify){
            return logn != null ? JsonData.buildSuccess(logn) : JsonData.buildError("数据格式异常");
        }else{
            return JsonData.buildError("密码错误");
        }

    }


    /**
     * @Author WYH
     * @Description //TODO 重置密码
     * @Date 下午 2:30 2022/3/7
     * @Param phone，password
     * @return JsonData
     **/
    @PostMapping("/forgetPassword")
    public JsonData forgetPassword(String phone, String password) {
        String md5 = Md5Util.MD5(password);
        Boolean forgetPassword = loginService.forgetPassword(phone, md5);
        return forgetPassword != false ? JsonData.buildSuccess(forgetPassword) : JsonData.buildError("重置失败");
    }
    
    /**
     * @Author WYH
     * @Description //TODO 创建用户
     * @Date 下午 2:06 2022/3/8
     * @Param nickname,phone,password
     * @return  JsonData
     **/
    @PostMapping("/createUser")
    public JsonData createUser(String nickname, String phone, String password){
        JsBeforeUser jsBeforeUser = new JsBeforeUser();
        Boolean user = false;
        if(loginService.getLogn(phone) == null){
            user = loginService.createUser(nickname, phone, password);
            if (user){
                jsBeforeUser = loginService.getLogn(phone);
            }
        }
        return jsBeforeUser != null ? JsonData.buildSuccess(user) : JsonData.buildError("创建失败");
    }

    /**
     * @Author WYH
     * @Description //TODO 快速登录
     * @Date 上午 10:29 2022/3/7
     * @Param phone
     * @return  JsonData
     **/
    @PostMapping("/getLoginByPhone")
    public JsonData getLoginByPhone(String phone){
        //验证码登录
        JsBeforeUser logn = loginService.getLogn(phone);
        //判断用户是否存在
        if(logn == null){
            Boolean quicklyRegisterUsers = loginService.quicklyRegisterUsers(phone);
            //判断添加是否成功
            if (quicklyRegisterUsers){
                logn = loginService.getLogn(phone);
            }
        }
        return logn != null ? JsonData.buildSuccess(logn) : JsonData.buildError("数据格式异常");
    }

    /**
     * @Author WYH
     * @Description //TODO 微信登录
     * @Date 下午 3:26 2022/3/10
     * @Param jsBeforeUser
     * @return JsonData
     **/
    @PostMapping("/getOraddWx")
    public JsonData getWx(JsBeforeUser jsBeforeUser){
        Integer wxId = loginService.getWx(jsBeforeUser.getOpenId());
        if (wxId == 0){
            Boolean addWxUser = loginService.addWxUser(jsBeforeUser);
            return addWxUser ? JsonData.buildSuccess(addWxUser) : JsonData.buildError("添加微信用户失败");
        }
        return wxId > 0 ? JsonData.buildSuccess(wxId) : JsonData.buildError("数据格式异常");
    }

    /**
     * @Author WYH
     * @Description //TODO QQ用户登录
     * @Date 下午 1:42 2022/3/11
     * @Param jsBeforeUser
     * @return JsonData
     **/
    @PostMapping("/getOraddQQ")
    public JsonData getOraddQQ(JsBeforeUser jsBeforeUser){
        Integer qqId = loginService.getQQ(jsBeforeUser.getOpenId());
        if (qqId == 0){
            Boolean addQQUser = loginService.addQQUser(jsBeforeUser);
            return addQQUser ? JsonData.buildSuccess(addQQUser) : JsonData.buildError("添加QQ用户失败");
        }
        return qqId > 0 ? JsonData.buildSuccess(qqId) : JsonData.buildError("数据格式异常");
    }

}
